﻿using System;
using System.Collections.Generic;
using System.Windows.Forms;
using Assist;
using Autodesk.Revit.Attributes;
using Autodesk.Revit.DB;
using Autodesk.Revit.UI;
using Autodesk.Revit.UI.Selection;

namespace HYTextTool
{
	[Transaction(TransactionMode.Manual)]
	[Regeneration(RegenerationOption.Manual)]
	public class CmdTextDraw : IExternalCommand
	{
		private void CreateViewSketchPlane(Autodesk.Revit.DB.View view, XYZ vtRight, XYZ vtUp, XYZ ptOrigin)
		{
			Plane plane = RevitVersionFuncs.CreatePlanByOriginAndBasis(ptOrigin, vtRight, vtUp);
			Transaction transaction = new Transaction(view.Document);
			transaction.Start("new sketchPlane");
			SketchPlane sketchPlane = RevitVersionFuncs.NewSketchPlane(view.Document, plane);
			view.SketchPlane = sketchPlane;
			transaction.Commit();
		}

		private bool ViewValidating(Document doc, Autodesk.Revit.DB.View activeView)
		{
			XYZ rightDirection = activeView.RightDirection;
			XYZ upDirection = activeView.UpDirection;
			XYZ origin = activeView.Origin;
			if (activeView is ViewPlan)
			{
				if ((activeView as Autodesk.Revit.DB.ViewPlan).SketchPlane == null)
				{
					this.CreateViewSketchPlane(activeView, rightDirection, upDirection, origin);
				}
				return true;
			}
			if (activeView is ViewSection)
			{
				if ((activeView as Autodesk.Revit.DB.ViewSection).SketchPlane == null)
				{
					this.CreateViewSketchPlane(activeView, rightDirection, upDirection, origin);
				}
				return true;
			}
			return activeView is ViewSheet;
		}

		public Result Execute(ExternalCommandData cmdData, ref string messages, ElementSet elements)
		{
			Document document = cmdData.Application.ActiveUIDocument.Document;
			Selection selection = cmdData.Application.ActiveUIDocument.Selection;
			TextTool textTool = new TextTool(cmdData);
			UIDocument activeUIDocument = cmdData.Application.ActiveUIDocument;
			if (!this.ViewValidating(activeUIDocument.Document, activeUIDocument.ActiveView))
			{
				MessageBox.Show("本功能不能在此视图下运行！");
				return Autodesk.Revit.UI.Result.Failed;
			}
			TextToolForm textToolForm = new TextToolForm();
			bool flag;
			while ((flag = (DialogResult.OK == textToolForm.ShowDialog())) && textToolForm.m_pickInScreen)
			{
				List<Text> list = new List<Text>();
				try
				{
					for (;;)
					{
						Reference reference = selection.PickObject(Autodesk.Revit.UI.Selection.ObjectType.Element, "请选择文本(Esc完成选择)");
						Element element = document.GetElement(reference);
						List<Text> textList = textTool.GetTextList(element);
						if (textList != null)
						{
							list.AddRange(textList);
						}
					}
				}
				catch
				{
					textToolForm.InsertText(list);
				}
			}
			if (!flag)
			{
				return Autodesk.Revit.UI.Result.Succeeded;
			}
			Result result;
			try
			{
				List<Row> rows = textTool.TransformSectionToRow(textToolForm.m_sections);
				List<Page> pages = textTool.TransformRowToPage(rows);
				XYZ ptInsert = selection.PickPoint("请选取插入点");
				textTool.SetTextLocation(ptInsert, pages);
				Transaction transaction = new Transaction(document, "DrawText");
				transaction.Start();
				try
				{
					textTool.DrawText(ptInsert, pages, textToolForm.m_scripts, textToolForm.m_aligns, textToolForm.m_length);
					transaction.Commit();
				}
				catch
				{
					transaction.RollBack();
					return Autodesk.Revit.UI.Result.Failed;
				}
				result = Autodesk.Revit.UI.Result.Succeeded;
			}
			catch
			{
				result = Autodesk.Revit.UI.Result.Failed;
			}
			return result;
		}
	}
}
